package hao.bk.searchMap;

import java.util.ArrayList;

import hao.bk.royalmap.GeneralAnimationUtils;
import hao.bk.royalmap.RoyalMapActivity;
import hao.bk.royalmap.SearchBarLayout;
import hao.bk.royalmap.R;
import hao.bk.sql.bo.Location;
import hao.bk.sql.dao.LocationDAO;
import hao.bk.utils.CharactersMark;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LocalShopInFromTabLayout extends LinearLayout implements
		OnItemClickListener,TextWatcher {

	private RoyalMapActivity main;
	/** Danh sách tất cả các shop lưu trong database */
	private ArrayList<Location> locationList;
	private ArrayList<Location> locationListShow;
	private ListView listView;
	protected SearchMapGeneralLayout generalLayout;
	private String preSeachedText;
	protected SearchBarLayout searchBarLayout;
	private TextView alertView;
	private int tabFromOrFav;

	public LocalShopInFromTabLayout (Context context) {
		super(context);
		initParams();
	}
	/**
	 * Khởi tạo các tham số
	 */
	private void initParams() {
		main = RoyalMapActivity.getInstance();
		this.generalLayout = main.searchMap.generalLayout;
		searchBarLayout = generalLayout.searchBarLayout;
		inflate(main,R.layout.local_shop_in_db_layout, this);
		listView = (ListView) findViewById(R.id.local_list_shop_db);
		listView.setOnItemClickListener(this);
		preSeachedText = "";
		alertView = (TextView) findViewById(R.id.searchmap_shop_alert_db);
		
	}
	/**
	 * Cập nhật danh sách của shop, location
	 */
	public void fillShops(int a) {
		// gán thông tin controll cho typeshop
		generalLayout.searchingAreaId = SearchMapGeneralLayout.SEARCHED_TYPESHOP_STATE;
		tabFromOrFav = a;
		// lấy danh sách location từ DB
		LocationDAO locationDAO = new LocationDAO(main);
		if(tabFromOrFav == 1) {
			//1 là tất cả các địa điểm
			locationList = locationDAO.getLocationInDb();
		} else if(tabFromOrFav == 2) {
			//2 là ds yêu thích
			locationList = locationDAO.getLocationsByFavorite();
		}
		
		
		// thiết lập TextWatch cho hộp tìm kiếm
		searchBarLayout.textEdt.addTextChangedListener(this);

		searchBarLayout.textEdt.setText("");			
		searchBarLayout.textEdt.setHint(main.getResources().getString(
			R.string.search_typeshop_hit));
		main.setOnBackPressedInterface(generalLayout);
		//pdateSearchTypeshop();  //test
		//ArrayList<Typeshop> typeshopListShow = new ArrayList<Typeshop>();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View arg1, int position,
			long arg3) {
		// ẩn bàn phím
		main.hiddenKeyboardForView(searchBarLayout.textEdt);
		searchBarLayout.textEdt.clearTextChangedListeners();
		Location location = locationListShow.get(position);
		LocalShopInformation localShopInfo = new LocalShopInformation(main, location,tabFromOrFav);// 1 thì từ tab hiện tại, 2 từ tab yêu thích
		GeneralAnimationUtils.startLeftToRight(this, localShopInfo);
		main.searchMap.addContent(localShopInfo);
	}
	/**
	 * @author haoDV Cập nhật danh sách tìm kiếm location
	 */

	public void updateSearchLocation() {
		preSeachedText = searchBarLayout.getText();
		String foundStr = CharactersMark.convertFoundString(
				preSeachedText.trim()).toLowerCase();
		Log.e("Ký tự đã tìm: ", preSeachedText);
		locationListShow = new ArrayList<Location>();
		String temp = "";
		if (foundStr != null && !foundStr.equals("")) {
			searchBarLayout.showClearBtn(true);
			for (int i = 0; i < locationList.size(); i++) {
				Location location = locationList.get(i);
				temp = location.getName();
				String inputStr = CharactersMark.convertFoundString(temp)
						.toLowerCase();
				if (inputStr.contains(foundStr)) {
					locationListShow.add(location);
				}
			}
		} else {
			locationListShow = locationList;
			searchBarLayout.showClearBtn(false);
		}
		if(locationListShow.isEmpty()){
			listView.setVisibility(GONE);
			alertView.setVisibility(VISIBLE);
		
		}else{
			listView.setVisibility(VISIBLE);
			alertView.setVisibility(GONE);
			listView.setAdapter(new LocalShopAdapter(main,
				R.id.local_list_shop_db, locationListShow));
		}
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		updateSearchLocation ();
	}
}

